﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Media;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class SlideShow : Form
    {
        int i;
        public int time;
        delegate void dSetPicture(Image aImage);
        delegate void dClose();
        Thread aThread;
        Form1 aForm1;
        public List<string> paths = new List<string>();
        public bool repeat;
        bool pause = false;
        static int controlWidth = 3 * 75 + 7 * 2;
        static int controlHeight = 2 * 75 + 6;
        int controlLeft = Screen.PrimaryScreen.Bounds.Width / 2 - controlWidth / 2;
        int controlRight = Screen.PrimaryScreen.Bounds.Width / 2 + controlWidth / 2;
        int controlTop = Screen.PrimaryScreen.Bounds.Height - (100 + 75 + 6 + 75);
        int controlBottom = Screen.PrimaryScreen.Bounds.Height - 100;
        Point oldPosition;
        public static System.Windows.Forms.Timer activityTimer = new System.Windows.Forms.Timer();
        bool mouseHidden = false;
        int oldx, oldy;
        public bool soundOn = false;
        public SoundPlayer sound;
        Thread newThread;
        bool firstRun = true;
        bool controlHidden = false;

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        public SlideShow(Form1 theForm1)
        {
            InitializeComponent();
            aForm1 = theForm1;

            button4.Top = Screen.PrimaryScreen.Bounds.Height - 75 - 25;
            button4.Left = Screen.PrimaryScreen.Bounds.Width - 75 - 75 - 75 - 25 - 6 - 6;

            button8.Top = Screen.PrimaryScreen.Bounds.Height - 75 - 25;
            button8.Left = Screen.PrimaryScreen.Bounds.Width - 75 - 75 - 25 - 6;

            button9.Top = Screen.PrimaryScreen.Bounds.Height - 75 - 25 - 75 - 6;
            button9.Left = Screen.PrimaryScreen.Bounds.Width - 75 - 75 - 25 - 6;

            button2.Top = Screen.PrimaryScreen.Bounds.Height - 75 - 25;
            button2.Left = Screen.PrimaryScreen.Bounds.Width / 2 - 387 / 2;

            button3.Top = Screen.PrimaryScreen.Bounds.Height - 75 - 25;
            button3.Left = Screen.PrimaryScreen.Bounds.Width - 75 - 25;

            button5.Top = 5;
            button5.Left = Screen.PrimaryScreen.Bounds.Width - 40 - 10;

            button1.Top = Screen.PrimaryScreen.Bounds.Height / 2 - 37;
            button1.Left = 25;

            button2.Image = Form1.resizeImage(button2.Image, 45, 45);
            button3.Image = Form1.resizeImage(button3.Image, 60, 40);
            button4.Image = Form1.resizeImage(button4.Image, 60, 40);
            button1.Image = Form1.resizeImage(button1.Image, 55, 55);
            button5.Image = Form1.resizeImage(button5.Image, 40, 15);
            button6.Image = Form1.resizeImage(button6.Image, 55, 55);

            activityTimer.Tick += activityWorker_Tick;
            activityTimer.Interval = 2000;
            activityTimer.Enabled = true;

            base.ProcessTabKey(false);
            /*
            string[] pathss = System.IO.Directory.GetFiles("C:\\Documents and Settings\\001\\Desktop\\New Folder (2)");

            foreach (string s in pathss)
            {
                if (s.Contains("Thumb"))
                {
                }
                else
                {
                    paths.Add(s);
                }
            }
             */

            button5.Image = Form1.resizeImage(button5.Image, 6, 2);

            button7.Left = Screen.PrimaryScreen.Bounds.Width - 20 - 5;
            button7.Top = 5;
            button8.Image = Form1.resizeImage(button8.Image, 40, 50);
            button9.Image = Form1.resizeImage(button9.Image, 40, 50);
        }

        public void showShow()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 1, 1, 0, 0);
            //newThread = new Thread(this.moveCursor);
            //newThread.Start();
            // Create a background thread and start it.
            aThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
            aThread.Start();
            i = 0;
            //Cursor.Position = new System.Drawing.Point((controlLeft+75+7+37), (controlTop+75+3));
        }

        private void moveCursor()
        {
            Thread.Sleep(2000);

            Cursor.Position = new System.Drawing.Point((controlLeft + 75 + 7 + 37), (controlBottom+1));
        }

        private void ThreadProcUnsafe()
        {
            while(i < (paths.Count()))
            {

                string path = paths[i];
                Bitmap aImage = new Bitmap(path);
                pictureBox1.Image = (Image)aImage;

                dSetPicture d = new dSetPicture(SetPicture);


                try
                {
                    this.Invoke(d, aImage);
                }
                catch
                {
                }

                Thread.Sleep(time);

                if (repeat && (i == (paths.Count() - 1)))
                {
                    i = -1;
                }

                i++;
            }

            dClose dc = new dClose(dClosing);

            try
            {
                Invoke(dc);
            }
            catch
            {
            }
        }


        private void SlideShow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                aThread.Abort();
                Close();
                activityTimer.Enabled = false;
                aForm1.WindowState = System.Windows.Forms.FormWindowState.Normal;
                Cursor.Show();
            }
            else if (e.KeyData == Keys.Space)
            {
                button2_Click(null, null);
            }
            else if (e.KeyData == Keys.Right)
            {
                button3_Click(null, null);
            }
            else if (e.KeyData == Keys.Left)
            {
                button4_Click(null, null);
            }
            else if (e.KeyData == Keys.Down)
            {
                button3_Click(null, null);
            }
            else if (e.KeyData == Keys.Up)
            {
                button4_Click(null, null);
            }
        }

        private void SetPicture(Image aImage)
        {
            pictureBox1.Height = aImage.Height;
            pictureBox1.Width = aImage.Width;

            while ((pictureBox1.Height > Screen.PrimaryScreen.Bounds.Height) || (pictureBox1.Width > Screen.PrimaryScreen.Bounds.Width))
            {
                pictureBox1.Height = (int)(pictureBox1.Height / 1.1);
                pictureBox1.Width = (int)(pictureBox1.Width / 1.1);
            }


            pictureBox1.Left = (Screen.PrimaryScreen.Bounds.Width / 2 - pictureBox1.Width / 2);
            pictureBox1.Top = (Screen.PrimaryScreen.Bounds.Height / 2 - pictureBox1.Height / 2);

            pictureBox1.Refresh();
            if (soundOn)
            {
                sound.Play();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            aThread.Abort();
            Close();
            activityTimer.Enabled = false;
            aForm1.WindowState = System.Windows.Forms.FormWindowState.Normal;
            Cursor.Show();
        }

        private void dClosing()
        {
            aThread.Abort();
            Close();
            activityTimer.Enabled = false;
            aForm1.WindowState = System.Windows.Forms.FormWindowState.Normal;
            Cursor.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pause)
            {
                i++;
                aThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
                aThread.Start();
                pause = false;
                Image temp = button6.Image;
                button6.Image = button2.Image;
                button2.Image = temp;
                button2.Text = "Pause [Space]";
            }
            else
            {
                aThread.Abort();
              //  paths.Clear();
                pause = true;
                Image temp = button6.Image;
                button6.Image = button2.Image;
                button2.Image = temp;
                button2.Text = "Play [Space]";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            i++;

            if (pause)
            {
                if (i < paths.Count())
                {
                    string path = paths[i];
                    Bitmap aImage = new Bitmap(path);
                    pictureBox1.Image = (Image)aImage;
                    SetPicture(aImage);
                }
                else if (i >= paths.Count())
                {
                    if (repeat)
                    {
                        i = 0;
                        string path = paths[i];
                        Bitmap aImage = new Bitmap(path);
                        pictureBox1.Image = (Image)aImage;
                        SetPicture(aImage);
                    }
                    else
                    {
                        aThread.Abort();
                        Close();
                        activityTimer.Enabled = false;
                        aForm1.WindowState = System.Windows.Forms.FormWindowState.Normal;
                        Cursor.Show();
                    }
                }
            }
            else
            {
                if (i < paths.Count())
                {
                    aThread.Abort();
                    aThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
                    aThread.Start();
                    System.Console.WriteLine(i);
                  //  paths.Clear();
                }
                else if (i >= paths.Count())
                {
                    if (repeat)
                    {
                        i = 0;
                        aThread.Abort();
                        aThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
                        aThread.Start();
                        System.Console.WriteLine(i);
                      //  paths.Clear();
                    }
                    else
                    {
                        aThread.Abort();
                        Close();
                        activityTimer.Enabled = false;
                        aForm1.WindowState = System.Windows.Forms.FormWindowState.Normal;
                        Cursor.Show();
                    }
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            i--;

            if (pause)
            {
                if (i >= 0)
                {
                    string path = paths[i];
                    Bitmap aImage = new Bitmap(path);
                    pictureBox1.Image = (Image)aImage;
                    SetPicture(aImage);
                }
                else
                {
                    if (repeat)
                    {
                        i = paths.Count() - 1;
                        string path = paths[i];
                        Bitmap aImage = new Bitmap(path);
                        pictureBox1.Image = (Image)aImage;
                        SetPicture(aImage);
                    }
                    else
                    {
                        aThread.Abort();
                        Close();
                        activityTimer.Enabled = false;
                        aForm1.WindowState = System.Windows.Forms.FormWindowState.Normal;
                        Cursor.Show();
                    }
                }
            }
            else
            {
                if (i >= 0)
                {
                    aThread.Abort();
                    aThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
                    aThread.Start();
                    System.Console.WriteLine(i);
                //    paths.Clear();
                }
                else
                {
                    if (repeat)
                    {
                        i = paths.Count() - 1;
                        aThread.Abort();
                        aThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
                        aThread.Start();
                        System.Console.WriteLine(i);
                     //   paths.Clear();
                    }
                    else
                    {
                        aThread.Abort();
                        Close();
                        activityTimer.Enabled = false;
                        aForm1.WindowState = System.Windows.Forms.FormWindowState.Normal;
                        Cursor.Show();
                    }
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (pause)
            {

            }
            else
            {
                aThread.Abort();
              //  paths.Clear();
                pause = true;
                Image temp = button6.Image;
                button6.Image = button2.Image;
                button2.Image = temp;
                button2.Text = "Play [Space]";
            }

            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
        }

        private void SlideShow_MouseMove(object sender, MouseEventArgs e)
        {
            if ((MousePosition.X != oldx) || (MousePosition.Y != oldy))
            {
                if (mouseHidden)
                {
                    Cursor.Show();
                    mouseHidden = false;
                    activityTimer.Start();
                    showControls();
                }
            }
            /*
            if ((MousePosition.X != (controlLeft + 75 + 7 + 37)) && (MousePosition.Y != (controlTop + 75 + 3)))
            {
                if (firstRun)
                {
                    newThread.Abort();
                    firstRun = false;
                }
            }
            */
            oldx = MousePosition.X;
            oldy = MousePosition.Y;

            /*
            if ((MousePosition.X <= controlRight) && (MousePosition.X >= controlLeft))
            {
                if ((MousePosition.Y >= controlTop) && (MousePosition.Y <= controlBottom))
                {
                    showControls();
                }
                else
                {
                    hideControls();
                }
            }
            else
            {
                hideControls();
            }
             */
        }

        private void hideControls()
        {
            button1.Visible = false;
            button2.Visible = false;
            button3.Visible = false;
            button4.Visible = false;
            button5.Visible = false;
            button7.Visible = false;
            button8.Visible = false;
            button9.Visible = false;
        }

        private void showControls()
        {
            button1.Visible = true;
            button2.Visible = true;
            button3.Visible = true;
            button4.Visible = true;
            button5.Visible = true;
            button7.Visible = true;
            button8.Visible = true;
            button9.Visible = true;
        }

        void activityWorker_Tick(object sender, EventArgs e)
        {
            if (!mouseHidden)
            {
                Cursor.Hide();
                activityTimer.Stop();
                mouseHidden = true;
                hideControls();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.WindowState = System.Windows.Forms.FormWindowState.Normal;
        }

        private void button3_MouseMove(object sender, MouseEventArgs e)
        {
            activityTimer.Stop();
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            activityTimer.Start();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            const int WM_KEYDOWN = 0x100;
            const int WM_SYSKEYDOWN = 0x104;

            if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
            {
                switch (keyData)
                {
                    case Keys.Right:
                        break;

                    case Keys.Left:
                        break;

                    case Keys.Up:
                        break;

                    case Keys.Down:
                        break;

                    case Keys.Space:
                        break;
                }
            }

            return true;
        }
    }
}
